<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cdkeymanager
 *
 * @author Dragon.hu 
 */
defined('BASEPATH') or die('forbidden');

class Cdkeymanager_Controller extends Controller {

    private $keytypeList;
    private $tmp_connect;

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        //激活码类型（1微信关注礼包、2 排名礼包、3 渠道礼包 6 集赞19礼包 7 集赞38礼包 8 集赞68礼包 9 集赞98礼包）
        $this->keytypeList = array('1' => '微信关注礼包', 
        		'2' => '排名礼包', '3' => '渠道礼包1', '4' => '渠道礼包2', '5' => '预留', 
        		'6' => '集赞19礼包', '7' => '集赞38礼包', '8' => '集赞68礼包', '9' => '集赞98礼包'
        );
        $this->tmp_connect = $this->load->database('center_db') or die("load failed!".mysql_error());
        
    }

    function index() {
        $this->view->display("activity/cdkeymanager/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        //$tmpsql = "SELECT distinct(keytype), starttime, endtime, rewardobjectid FROM cdkey ";
        $sql = "SELECT COUNT(*) AS allnum,c.* FROM cdkey c GROUP BY keytype ";
        $this->tmp_connect->query($sql) or die("query failed!".mysql_error());
        $rows = array();
        $i = 1;
        while($dynrow = $this->tmp_connect->get_array()){
        	$tmpsql = "SELECT COUNT(*) AS usenum FROM cdkey WHERE isuse = 1 and keytype = ".$dynrow['keytype'];
        	$one = $this->tmp_connect->one($tmpsql);
        	$dynrow['usenum'] = $one['usenum'];
        	$dynrow['keytype'] = $this->keytypeList[$dynrow['keytype']]."(".$dynrow['keytype'].")";
        	$rows[] = array('id' => $dynrow['id'], 'cell' => $dynrow);
        }
        //print_r($rows);
        //exit(0);

        $total = count($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function edit($id = 0) {
        if (is_submit()) {
            $row = $_POST['row'];
            //print_r($row);
            //exit(0);
            $tmp = "SELECT keytype FROM cdkey WHERE id = ".$id;
            $this->tmp_connect->query($tmp) or die("query failed!".mysql_errno());
            $one = $this->tmp_connect->get_array();
            //print_r($one);
            //exit(0);
            $newsql = "UPDATE cdkey SET starttime = '".$row['starttime']."',endtime = '".$row['endtime']."', rewardobjectid = ".$row['rewardobjectid']." WHERE keytype = ".$one['keytype'];
            $res = $this->tmp_connect->execute($newsql);
            cp_tips ( true == $res ? "success" : "error" );
        }
        $tmpsql = "SELECT * FROM cdkey WHERE id = ".$id;
        $this->tmp_connect->query($tmpsql) or die("query failed!".mysql_error());
        $row = $this->tmp_connect->get_array();
        $row['keytype'] = $this->keytypeList[$row['keytype']];
        $this->view->assign("row", $row);
        $this->view->display("activity/cdkeymanager/edit");
    }
    

}

?>
